<?php
use think\Db;
use com\IpLocation;

use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;//短信发送记录查询使用
use org\QRcode;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

header("Content-type: text/html; charset=utf-8");

function systembug($content,$type=1,$url){
    Db::name('system_bug')->insert(['content'=>$content,'type'=>$type,'url'=>$url,'addtime'=>date('Y-m-d H:i:s')]);
}
/**
 * 判断某年的某月有多少天
 * @return [type] [description]
 */
function getDaysInMonth($year = '', $month = '')
{
    if (empty($year)) $year = date('Y');
    if (empty($month)) $month = date('m');
    if (in_array($month, array(1, 3, 5, 7, 8, '01', '03', '05', '07', '08', 10, 12))) {
        $text = '31';//月大
    } elseif ($month == 2 || $month == '02') {
        if (($year % 400 == 0) || (($year % 4 == 0) && ($year % 100 !== 0))) {//判断是否是闰年
            $text = '29';//闰年2月
        } else {
            $text = '28';//平年2月
        }
    } else {
        $text = '30';//月小
    }
    return $text;
}

function get_week($date){
    //强制转换日期格式
    $date_str=date('Y-m-d',strtotime($date));

    //封装成数组
    $arr=explode("-", $date_str);

    //参数赋值
    //年
    $year=$arr[0];

    //月，输出2位整型，不够2位右对齐
    $month=sprintf('%02d',$arr[1]);

    //日，输出2位整型，不够2位右对齐
    $day=sprintf('%02d',$arr[2]);

    //时分秒默认赋值为0；
    $hour = $minute = $second = 0;

    //转换成时间戳
    $strap = mktime($hour,$minute,$second,$month,$day,$year);

    //获取数字型星期几
    $number_wk=date("w",$strap);

    //自定义星期数组
    $weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");

    //获取数字对应的星期
    return $weekArr[$number_wk];
}

/**
 * 添加修为记录
 * @param  [mid] $mid   [用户id]
 * @param  [quality] $quality   [要增加的修为值]
 * @param  [ftype] 1助人为乐 2精进修持 3寺院互动 4浏览内容 5账号信息
 * @param  [type] $type  1替人解惑:3积分,2发布疑惑:5,3查看疑惑:1,4分享疑惑2,5赞同疑惑  6早课进修 7晚课进修 8十念定课 9一心专念 10修行念珠 11许愿祈福 12发露忏悔 13分享每日一禅 14邀请佛友 15分享寺院 16绑定寺院 17绑定法师 18浏览寺院 19收藏寺院 20佛事预约 21转发分享 22评论资讯 23查看咨询 24点赞资讯 25 搜索内容 26开智慧 27听禅乐 28完善资料 29绑定微信 30绑定QQ 31意见反馈 32上传头像 33修改昵称 34设置密码 [修为值的类型]
 */
function add_quality($mid,$type,$userquality,$ftype)
{
    $member = Db::name('member')->where('id',$mid)->find();
    if($member['eqno'] == 'zh_temple' && empty($member['tel'])){
       return false;
    }
    $num = Db::name('member_quality')->where(['mid'=>$mid,'type'=>$type,'create_time'=>['between',[strtotime(date('Y-m-d')),strtotime(date('Y-m-d'))+24*3600]]])->count();
    $config = get_config();
    if($ftype == 1){
        $typenum = unserialize($config['help_people']);
        if(empty($typenum)){
            $typenum = config('quality.help_people');
        }
    }elseif($ftype == 2){
        $typenum = unserialize($config['work_hard']);
        if(empty($typenum)){
            $typenum = config('quality.work_hard');
        }
    }elseif($ftype == 3){
        $typenum = unserialize($config['temple_interaction']);
        if(empty($typenum)){
            $typenum = config('quality.temple_interaction');
        }
    }elseif($ftype == 4){
        $typenum = unserialize($config['glance_content']);
        if(empty($typenum)){
            $typenum = config('quality.glance_content');
        }
    }elseif($ftype == 5){
        $typenum = unserialize($config['account_information']);
        if(empty($typenum)){
            $typenum = config('quality.account_information');
        }
    }
    foreach($typenum as &$v){
        if($v['type'] == $type){
            $quality_array = $v;
        }
    }
    if($type == $quality_array['type'] && $num >= $quality_array['num']){
        return false;
    }

    $data['mid'] = $mid;
    $data['quality'] = $quality_array['quality'];
    $data['type'] = $type;
    $data['create_time'] = time();
    Db::name('member_quality')->insert($data);
    Db::name('member')->where('id',$mid)->update(['quality'=>$userquality + $data['quality']]);
}

function getipaddress($ip)
{
    $url="http://api.map.baidu.com/location/ip?ip=$ip&ak=nawaqWC9kK2GzWppqqWkLUdgxsP82KxW";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //参数为1表示传输数据，为0表示直接输出显示。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //参数为0表示不带头文件，为1表示带头文件
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
/*
     移动：134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
     联通：130、131、132、152、155、156、185、186
     电信：133、153、180、189、（1349卫通）
     */
/**
 *   手机服务商函数 getphonetype
 *@author by @tianxiao
 *@param  string  $phone   手机号字符串
 *@return  unsignedint   1中国移动，2中国联通  3中国电信  4未知
 **/
function getphonetype($phone){
    $phone = trim($phone);
    $isChinaMobile = "/^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\d{8}$/"; //移动方面最新答复
    $isChinaUnion = "/^(?:13[0-2]|145|15[56]|176|18[56])\d{8}$/"; //向联通微博确认并未回复
    $isChinaTelcom = "/^(?:133|153|177|173|18[019])\d{8}$/"; //1349号段 电信方面没给出答复，视作不存在
    // $isOtherTelphone = "/^170([059])\\d{7}$/";//其他运营商
    if(preg_match($isChinaMobile, $phone)){
        return 1;
    }elseif(preg_match($isChinaUnion, $phone)){
        return 2;
    }elseif(preg_match($isChinaTelcom, $phone)){
        return 3;
    }else{
        return 4;
    }
}

/**

 * 根据手机号码获取归属地

 */

function getMobileLoc($mobile)
{

    $data = array('province' => '', 'city' => '');

     //根据百度的数据库调用返回值

     header("Content-type:text/html;charset=utf-8");

     $url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={$mobile}&resource_id=6004&ie=utf8&oe=utf8&format=json";

     $result = curl_https($url);

     $result = (array)json_decode($result);

     if (!empty($result['data'])) {

         $result = (array)$result['data'][0];

         $data['city'] = empty($result['city']) ? '' : $result['city'];

         $data['province'] = empty($result['prov']) ? $result['city'] : $result['prov'];

     }
     return $data;
}
/**

 * curl请求

 */

function curl_https($url){

     $ch = curl_init();

     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

     curl_setopt($ch, CURLOPT_HEADER, FALSE);

     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

     curl_setopt($ch, CURLOPT_URL, $url);

     curl_setopt($ch, CURLOPT_REFERER, $url);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

     $result = curl_exec($ch);

     curl_close($ch);



     //-------请求为空

     if(empty($result)){

         return false;

     }
     return $result;

}

function filter_Emoji($str)
{
    $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);

    return $str;
}
/**
 * [getlocalimg 把微博头像存储起来]
 * @param
 * @author [C。]
 * @return json
 */
function saveImageFromHttp($imgFromUrl) {
    //取数组最后一个元素，得到文件类型
    $uploaddir = "wheader/";//设置文件保存目录 注意包含
    if (!file_exists($uploaddir)) {
        mkdir($uploaddir, 0755, true);
    }
    //得到 $imgFromUrl 的图片格式
    $ext = strrchr($imgFromUrl, ".");
    //如果图片格式不为.gif 或者.jpg .png，直接退出即可
    if ($ext != ".gif" && $ext != ".jpg" && $ext != 'png'){
        return false;
    }
    $newFileName = $uploaddir.date("dMYHis") . $ext;
    //用天月面时分秒来命名新的文件名

    ob_start();//打开输出
    readfile($imgFromUrl);//输出图片文件
    $img = ob_get_contents();//得到浏览器输出
    ob_end_clean();//清除输出并关闭
    //$size = strlen($img);//得到图片大小
    $fp2 = @fopen($newFileName, "a");
    fwrite($fp2, $img);//向当前目录写入图片文件，并重新命名
    fclose($fp2);
    return $newFileName;//返回新的文件名
}
/**
 * [getlocalimg 把微信头像存储起来]
 * @param
 * @author [C。]
 * @return json
 */
function getlocalimg($imgurl)
{
    //取数组最后一个元素，得到文件类型
    $uploaddir = "wheader/";//设置文件保存目录 注意包含
    if (!file_exists($uploaddir)) {
        mkdir($uploaddir, 0755, true);
    }

    $header = array(
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
        'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
        'Accept-Encoding: gzip, deflate',);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $imgurl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    $data = curl_exec($curl);
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    if ($code == 200) {//把URL格式的图片转成base64_encode格式的！
        $imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
    }

    $img_content=$imgBase64Code;//图片内容

    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result))
    {
        $type = $result[2];//得到图片类型png?jpg?gif?
        $new_file = $uploaddir . md5(uniqid(rand())) . '.' . $type; //产生随机文件名
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content))))
        {
            return $new_file;
//            $filename = uploadoss($new_file,$dir,$id);
//            return $filename;
        }else{
            return $imgurl;
        }
    }else{
        return $imgurl;
    }
}

function uploadoss($path,$dir='',$id = 0){
    $accessKeyId = 'LTAIUZZ0EO0Xlz0c';
    $accessKeySecret = 'ZPPOxFBERpKvaw4uO5oZVbdgnd5cRd';
    // Endpoint以杭州为例，其它Region请按实际情况填写。
    $endpoint = 'http://oss-cn-hangzhou.aliyuncs.com';
    $bucket= 'fushan';
    // 文件名称
    if(empty($dir)){
        $object = 'FS'.$id.md5(uniqid(rand()));
    }else{
        $object = $dir.'/FS'.$id.md5(uniqid(rand()));
    }

    // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
    $filePath = $path;

    try{
        $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $ossClient->uploadFile($bucket, $object, $filePath);
        $ossClient = (array)$ossClient;
    } catch(OssException $e) {
        return admin_json(-1,$e->getMessage());
    }
    unlink($filePath);
    return array_values($ossClient)[4];
}
/**
 * 发布时间提示
 * @param  integer $len 长度
 * @return string
 */
function get_last_time($time)
{
    // 当天最大时间
    $todayLast = strtotime(date('Y-m-d 23:59:59'));
    $agoTimeTrue = time() - $time;
    $agoTime = $todayLast - $time;
    $agoDay = floor($agoTime / 86400);

    if ($agoTimeTrue < 60) {
        $result = '刚刚';
    } elseif ($agoTimeTrue < 3600) {
        $result = (ceil($agoTimeTrue / 60)) . '分钟前';
    } elseif ($agoTimeTrue < 3600 * 12) {
        $result = (ceil($agoTimeTrue / 3600)) . '小时前';
    } elseif ($agoDay == 1) {
        $result = '昨天 ';
    } elseif ($agoDay == 2) {
        $result = '前天 ';
    } else {
        $format = date('Y') != date('Y', $time) ? "Y-m-d" : "m-d";
        $result = date($format, $time);
    }
    return $result;
}
/**
 * [encryptDecrypt  加密解密]
 * @param $key 密钥
 * @param $string 加密字符串
 * @param $key $decrypt 0加密 1解密
 * @return json
 * @author [C。]
 */
function encryptDecrypt($key, $string, $decrypt){
    if($decrypt){
        $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");
        return $decrypted;
    }else{
        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
        return $encrypted;
    }
}

/**
 * [del  图片删除]
 * @param
 * @return json
 * @author [C。]
 */
function ossdel($path)
{
    $accessKeyId = 'LTAIUZZ0EO0Xlz0c';
    $accessKeySecret = 'ZPPOxFBERpKvaw4uO5oZVbdgnd5cRd';
    // Endpoint以杭州为例，其它Region请按实际情况填写。
    $endpoint = 'http://oss-cn-hangzhou.aliyuncs.com';
    $bucket= 'fushan';
    $object = $path;
    $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $ossClient->deleteObject($bucket, $object);

}

function get_img_thumb_url($content="",$suffix){
//    $pregRule = "/<[img|IMG].*?src=[\'|\"]/";
    $pregRule = '/<img src="\/Public\/kindeditor\/attached\/image/';
    $content = preg_replace($pregRule, '<img src="'.$suffix.'Public/kindeditor/attached/image', $content);
    $content = str_replace('http://www.fushan8.com','http://pic.fushan8.com',$content);
    return $content;
}
/**
把用户输入的文本转义（主要针对特殊符号和emoji表情）
 */
function userTextEncode($str){
    if(!is_string($str))return $str;
    if(!$str || $str=='undefined')return '';

    $text = json_encode($str); //暴露出unicode
    $text = preg_replace_callback("/(\\\u[ed][0-9a-f]{3})/i",function($str){
        return addslashes($str[0]);
    },$text); //将emoji的unicode留下，其他不动，这里的正则比原答案增加了d，因为我发现我很多emoji实际上是\ud开头的，反而暂时没发现有\ue开头。
    return json_decode($text);
}

/**
解码上面的转义
 */
function userTextDecode($str){
    $text = json_encode($str); //暴露出unicode
    $text = preg_replace_callback('/\\\\\\\\/i',function($str){
        return '\\';
    },$text); //将两条斜杠变成一条，其他不动
    return json_decode($text);
}

/**
 * 获取随机位数数字
 * @param  integer $len 长度
 * @return string
 */
function randString($len = 6) {
    $chars = str_repeat('0123456789', $len);
    $chars = str_shuffle($chars);
    $str = substr($chars, 0, $len);
    return $str;
}

/**
 * 内容转化特殊字符
 * @param  integer $len 长度
 * @return string
 */
function percent($data) {
    $data = str_replace('&lt;','<',$data);
    $data = str_replace('&gt;','>',$data);
    $data = str_replace('&quot;','"',$data);
    $data = str_replace('nbsp;','&nbsp;',$data);
    $data = str_replace('&&nbsp;','&nbsp;',$data);
    $data = str_replace('&amp;',' ',$data);
    return $data;
}
/**
 * [aliyun_sendsms  门票审核拒绝短息]
 * @author [忘尘]
 * @param $mobile 接收手机号
 * @param $jing 景区名
 * @param $rmk 拒绝原因
 * @param string $tplCode   短信模板ID
 * @return array
 */
function aliyun_sendsms($mobile,$jing,$rmk)
{
    if( empty($mobile))
        return array('msg'=>'缺少参数','code'=>'Error');

    require_once '../extend/aliyundysmssdk/api_sdk/vendor/autoload.php';
    // 加载区域结点配置
    Config::load();
    //产品名称:云通信流量服务API产品,开发者无需替换
    $product = "Dysmsapi";
    //产品域名,开发者无需替换
    $domain = "dysmsapi.aliyuncs.com";

    //配置文件
    $config = get_config();
    $accessKeyId = $config['alisms_appkey']; // AccessKeyId
    $accessKeySecret = $config['alisms_appsecret']; // AccessKeySecret
    $tplCode = $config['refuse'];
    // 暂时不支持多Region
    $region = "cn-hangzhou";
    // 服务结点
    $endPointName = "cn-hangzhou";
    //初始化acsClient,暂不支持region化
    $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
    // 增加服务结点
    DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
    // 初始化AcsClient用于发起请求
    $acsClient = new DefaultAcsClient($profile);
    // 初始化SendSmsRequest实例用于设置发送短信的参数
    $request = new SendSmsRequest();
    // 必填，设置短信接收号码
    $request->setPhoneNumbers($mobile);
    // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
    $request->setSignName($config['alisms_signname']);

    // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
    $request->setTemplateCode($tplCode ? $tplCode : $config['alisms_templatecode']);

    // 可选，设置模板参数, 假如模板中存在变量需要替换则为必填项
    $request->setTemplateParam(json_encode(array(  // 短信模板中字段的值
        "jing"=>$jing,
        "rmk"=>$rmk,
    ), JSON_UNESCAPED_UNICODE));

    // 可选，设置流水号
    $request->setOutId("yourOutId");

    // 选填，上行短信扩展码（扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段）
    $request->setSmsUpExtendCode("1234567");

    // 发起访问请求
    $acsResponse = $acsClient->getAcsResponse($request);

    //返回请求结果
    $result = json_decode(json_encode($acsResponse),true);
    $result['tel'] = $mobile;
    return $result;
}
/**
 * [aliyun_sendsms  示例 阿里云云通信发送短息]
 * @author [忘尘]
 * @param $mobile 接收手机号
 * @param $jing 景区名
 * @param string $tplCode   短信模板ID
 * @return array
 */
function advice_sendsms($mobile,$jing,$time,$name)
{
    if( empty($mobile))
        return array('msg'=>'缺少参数','code'=>'Error');

    require_once '../extend/aliyundysmssdk/api_sdk/vendor/autoload.php';
    // 加载区域结点配置
    Config::load();
    //产品名称:云通信流量服务API产品,开发者无需替换
    $product = "Dysmsapi";
    //产品域名,开发者无需替换
    $domain = "dysmsapi.aliyuncs.com";

    //配置文件
    $config = get_config();
    $accessKeyId = $config['alisms_appkey']; // AccessKeyId
    $accessKeySecret = $config['alisms_appsecret']; // AccessKeySecret
    $tplCode = $config['inform'];
    // 暂时不支持多Region
    $region = "cn-hangzhou";
    // 服务结点
    $endPointName = "cn-hangzhou";
    //初始化acsClient,暂不支持region化
    $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
    // 增加服务结点
    DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
    // 初始化AcsClient用于发起请求
    $acsClient = new DefaultAcsClient($profile);
    // 初始化SendSmsRequest实例用于设置发送短信的参数
    $request = new SendSmsRequest();
    // 必填，设置短信接收号码
    $request->setPhoneNumbers($mobile);
    // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
    $request->setSignName($config['alisms_signname']);

    // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
    $request->setTemplateCode($tplCode ? $tplCode : $config['alisms_templatecode']);

    // 可选，设置模板参数, 假如模板中存在变量需要替换则为必填项
    $request->setTemplateParam(json_encode(array(  // 短信模板中字段的值
        "jing"=>$jing,
        "time"=>$time,
        "name"=>$name,
    ), JSON_UNESCAPED_UNICODE));

    // 可选，设置流水号
    $request->setOutId("yourOutId");

    // 选填，上行短信扩展码（扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段）
    $request->setSmsUpExtendCode("1234567");

    // 发起访问请求
    $acsResponse = $acsClient->getAcsResponse($request);

    //返回请求结果
    $result = json_decode(json_encode($acsResponse),true);
    $result['tel'] = $mobile;
    return $result;
}
/**
 * [aliyun_sendsms  示例 阿里云云通信发送短息]
 * @author [忘尘]
 * @param $mobile 接收手机号
 * @param $code 验证码
 * @param string $tplCode   短信模板ID
 * @return array
 */
function demo_sendsms($mobile,$code)
{
    if( empty($mobile))
        return array('msg'=>'缺少参数','code'=>'Error');

    require_once '../extend/aliyundysmssdk/api_sdk/vendor/autoload.php';
    // 加载区域结点配置
    Config::load();
    //产品名称:云通信流量服务API产品,开发者无需替换
    $product = "Dysmsapi";
    //产品域名,开发者无需替换
    $domain = "dysmsapi.aliyuncs.com";

    //配置文件
    $config = get_config();
    $accessKeyId = $config['alisms_appkey']; // AccessKeyId
    $accessKeySecret = $config['alisms_appsecret']; // AccessKeySecret
    $tplCode = $config['tplCode'];
    // 暂时不支持多Region
    $region = "cn-hangzhou";
    // 服务结点
    $endPointName = "cn-hangzhou";
    //初始化acsClient,暂不支持region化
    $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
    // 增加服务结点
    DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
    // 初始化AcsClient用于发起请求
    $acsClient = new DefaultAcsClient($profile);
    // 初始化SendSmsRequest实例用于设置发送短信的参数
    $request = new SendSmsRequest();
    // 必填，设置短信接收号码
    $request->setPhoneNumbers($mobile);
    // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
    $request->setSignName($config['alisms_signname']);

    // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
    $request->setTemplateCode($tplCode ? $tplCode : $config['alisms_templatecode']);

    // 可选，设置模板参数, 假如模板中存在变量需要替换则为必填项
    $request->setTemplateParam(json_encode(array(  // 短信模板中字段的值
        "code"=>$code,
        "product"=>"福善在线"
    ), JSON_UNESCAPED_UNICODE));

    // 可选，设置流水号
    $request->setOutId("yourOutId");

    // 选填，上行短信扩展码（扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段）
    $request->setSmsUpExtendCode("1234567");

    // 发起访问请求
    $acsResponse = $acsClient->getAcsResponse($request);

    //返回请求结果
    $result = json_decode(json_encode($acsResponse),true);

    return $result;
}


/**
 * [get_config  获取配置]
 * @author [忘尘]
 * @return array
 */
function get_config(){
    $list = Db::name('config')->select();
    $config = [];
    foreach ($list as $k => $v) {
        $config[trim($v['cname'])] = $v['cvalue'];
    }
    return $config;
}

/**
 * 邮件发送
 * @param $to    接收人
 * @param string $subject   邮件标题
 * @param string $content   邮件内容(html模板渲染后的内容)
 * @throws Exception
 * @throws phpmailerException
 *
 * 调用前头部引入
 * use PHPMailer\PHPMailer\PHPMailer;
 * use PHPMailer\PHPMailer\Exception;
 */
function send_email($to,$subject='',$content=''){

    //判断openssl是否开启
    $openssl_funcs = get_extension_funcs('openssl');
    if(!$openssl_funcs){
        return array('status'=>-1 , 'msg'=>'请先开启openssl扩展');
    }
    $mail = new PHPMailer;
    //配置信息
    $config = get_config();
    if(empty($config['smtp_server']) || empty($config['smtp_port']) || empty($config['smtp_user']) || empty($config['smtp_pwd'])){
        return array("error"=>1,"message"=>'邮箱配置不完整');
    }

    $mail->CharSet  = 'UTF-8'; //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
    $mail->isSMTP();
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 0;
    //调试输出格式
    //$mail->Debugoutput = 'html';
    //smtp服务器
    $mail->Host = $config['smtp_server'];
    //端口 - likely to be 25, 465 or 587
    $mail->Port = $config['smtp_port'];

    if($mail->Port == 465) $mail->SMTPSecure = 'ssl';// 使用安全协议
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //用户名
    $mail->Username = $config['smtp_user'];
    //密码
    $mail->Password = $config['smtp_pwd'];
    //Set who the message is to be sent from
    $mail->setFrom($config['smtp_user']);
    //回复地址
    $mail->addReplyTo($config['smtp_user'], '忘尘');
    //设置抄送人
    //$mail->addCC($config['']);
    //密送者，Mail Header不会显示密送者信息
    //$mail->addBCC($config['']);

    //接收邮件方
    if(is_array($to)){
        foreach ($to as $v){
            $mail->addAddress($v);
        }
    }else{
        $mail->addAddress($to);
    }
    $mail->isHTML(true);// send as HTML
    //标题
    $mail->Subject = $subject;
    //HTML内容转换
    $mail->msgHTML($content);
    //Replace the plain text body with one created manually
    //$mail->AltBody = 'This is a plain-text message body';
    //添加附件 (填绝对路径) 可以设定名字
    //$mail->addAttachment('images/phpmailer_mini.png', 'name.jpg');
    //send the message, check for errors
    try{
        if($mail->send());
        return array('status'=>1 , 'msg'=>'邮件发送成功');
    } catch (Exception $e) {
        return array('status'=>-1 , 'msg'=>'发送失败: '.$mail->ErrorInfo);
    }
}





/**
 * 字符串截取，支持中文和其他编码
 */
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
    if (function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
        if (false === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
    }
    return $suffix ? $slice . '...' : $slice;
}



/**
 * 读取配置
 * @return array
 */
function load_config(){
    $list = Db::name('config')->select();
    $config = [];
    foreach ($list as $k => $v) {
        $config[trim($v['name'])]=$v['value'];
    }

    return $config;
}


/**
 * 验证手机号是否正确
 * @author honfei
 * @param number $mobile
 */
function isMobile($mobile) {
    if (!is_numeric($mobile)) {
        return false;
    }
    return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8,9]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
}




//生成网址的二维码 返回图片地址demo_sendsms
function Qrcode($token, $url, $size = 8){
    $md5 = md5($token);
    $dir = date('Ymd'). '/' . substr($md5, 0, 10) . '/';
    $patch = 'qrcode/' . $dir;
    if (!file_exists($patch)){
        mkdir($patch, 0755, true);
    }
    $file = 'qrcode/' . $dir . $md5 . '.png';
    $fileName =  $file;
    if (!file_exists($fileName)) {

        $level = 'L';
        $data = $url;
        QRcode::png($data, $fileName, $level, $size, 2, true);
    }
    return $file;
}



/**
 * 循环删除目录和文件
 * @param string $dir_name
 * @return bool
 */
function delete_dir_file($dir_name) {
    $result = false;
    if(is_dir($dir_name)){
        if ($handle = opendir($dir_name)) {
            while (false !== ($item = readdir($handle))) {
                if ($item != '.' && $item != '..') {
                    if (is_dir($dir_name . DS . $item)) {
                        delete_dir_file($dir_name . DS . $item);
                    } else {
                        unlink($dir_name . DS . $item);
                    }
                }
            }
            closedir($handle);
            if (rmdir($dir_name)) {
                $result = true;
            }
        }
    }

    return $result;
}

/**
 * [make_dir  创建目录]
 * @author [忘尘]
 * @param $path
 * @param int $mode
 * @param bool $recursive
 * @return bool
 */
function make_dir($path , $mode = 0755, $recursive = true)
{
    if(!is_dir($path))
    {
        mkdir($path,$mode,$recursive);
        chmod($path,$mode);
    }
    return true;
}



//时间格式化1
function formatTime($time) {
    $now_time = time();
    $t = $now_time - $time;
    $mon = (int) ($t / (86400 * 30));
    if ($mon >= 1) {
        return '一个月前';
    }
    $day = (int) ($t / 86400);
    if ($day >= 1) {
        return $day . '天前';
    }
    $h = (int) ($t / 3600);
    if ($h >= 1) {
        return $h . '小时前';
    }
    $min = (int) ($t / 60);
    if ($min >= 1) {
        return $min . '分钟前';
    }
    return '刚刚';
}


//时间格式化2
function pincheTime($time) {
    $today  =  strtotime(date('Y-m-d')); //今天零点
    $here   =  (int)(($time - $today)/86400) ;
    if($here==1){
        return '明天';
    }
    if($here==2) {
        return '后天';
    }
    if($here>=3 && $here<7){
        return $here.'天后';
    }
    if($here>=7 && $here<30){
        return '一周后';
    }
    if($here>=30 && $here<365){
        return '一个月后';
    }
    if($here>=365){
        $r = (int)($here/365).'年后';
        return   $r;
    }
    return '今天';
}


function getRandomString($len, $chars=null){
    if (is_null($chars)){
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    }
    mt_srand(10000000*(double)microtime());
    for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
        $str .= $chars[mt_rand(0, $lc)];
    }
    return $str;
}


function random_str($length){
    //生成一个包含 大写英文字母, 小写英文字母, 数字 的数组
    $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));

    $str = '';
    $arr_len = count($arr);
    for ($i = 0; $i < $length; $i++)
    {
        $rand = mt_rand(0, $arr_len-1);
        $str.=$arr[$rand];
    }

    return $str;
}

/**
 * 通用化API接口数据输出
 * [api_show  ]
 * @author [默默]
 * @param $status 业务状态码
 * @param $message 信息提示
 * @param array $data 数据
 * @param int $httpCode http状态码
 * @return \think\response\Json
 */
function api_show($status, $message, $data=[], $httpCode=200) {

    $jsondata = [
        'status' => $status,
        'msg' => $message,
        'data' => $data,
    ];
    return json($jsondata, $httpCode);
}

/*验证$ip地址函数,真IP返回true,假IP返回false*/
function CheckIsIP($ip){
    return !strcmp(long2ip(sprintf('%u',ip2long($ip))),$ip) ? true : false;
}

/*获得客户端ip地址,调用方法getIP()*/
function getIP() {
    if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown")) {
        $ip = getenv("HTTP_CLIENT_IP");
    }
    else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),"unknown")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    }
    else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"),"unknown")) {
        $ip = getenv("REMOTE_ADDR");
    }
    else if(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],"unknown")) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    else {
        $ip = "unknown";
    }
    return CheckIsIP($ip) ? $ip : "unknown" ;
}

//*************************************字符串处理函数集***********************************
/*utf8中文字串截取函数*/
function strcut($str,$start,$len){
    if($start < 0)
        $start = strlen($str)+$start;
    $retstart = $start+getOfFirstIndex($str,$start);
    $retend = $start + $len -1 + getOfFirstIndex($str,$start + $len);
    return substr($str,$retstart,$retend-$retstart+1);
}
//判断字符开始的位置
function getOfFirstIndex($str,$start){
    $char_aci = ord(substr($str,$start-1,1));
    if(223<$char_aci && $char_aci<240)
        return -1;
    $char_aci = ord(substr($str,$start-2,1));
    if(223<$char_aci && $char_aci<240)
        return -2;
    return 0;
}

/*$num必须为英文字符或数字0-9*/
function getNums($num) {
    return (ctype_alnum($num));
}
/*匹配电子邮件地址*/
function getEmail($email) {
    return strlen($email)>6 && preg_match("/^\w+@(\w+\.)+[com]|[cn]$/" , $email);
}
/*生成email连接*/
function emailconv($email,$tolink=1) {
    $email=str_replace(array('@','.'),array('@','.'),$email);
    return $tolink ? '<a href="mailto: '.$email.'">'.$email.'</a>':$email;
}

/*$char必须为英文字符,全是返回1true，有中文返回0false*/
function getChar($char) {
    return (ctype_alpha($char));
}
/*匹配$QQ(5-12)位,是返回1,否返回0*/
function getQQ($QQ) {
    return preg_match("/^\b[0-9]{5,12}\b/",$QQ);
}
/*检查$str是否为数字,是返回true,否返回false*/
function CheckIsNum($str){
    return ereg("^[0-9]+$",$str) ? true : false;
}


/*容量大小计算函数*/
function sizecount($filesize) {
    if($filesize >= 1073741824) {
        $filesize = round($filesize / 1073741824 * 100) / 100 . ' G'; //round 四舍五入
    } elseif($filesize >= 1048576) {
        $filesize = round($filesize / 1048576 * 100) / 100 . ' M';
    } elseif($filesize >= 1024) {
        $filesize = round($filesize / 1024 * 100) / 100 . ' K';
    } else {
        $filesize = $filesize . ' bytes';
    }
    return $filesize;
}

/**
 * [home_log  前台日志]
 * @author [忘尘]
 * @param array $logdata
 * @return int|string
 * @throws Exception
 */
function home_log($content)
{
    if(empty($content))
        exception('参数不正确');

    $logdata=array(
        'user_id'=> !empty(cache('user')) ? cache('user.id') : 2,//2为游客标识
        'click_ip' => ip2long(getIP()),
        'os_broswer' => get_os().'--'.get_broswer(),
        'do_content' => $content,
        'click_time' => time()
    );

    $res = Db::name('log_home')->insert($logdata);
    return $res;
}

/**
 * [create_qrcode  生成二维码]
 * @author [忘尘]
 * @param $text 二维码内容
 * @param bool $filename 生成二维码文件名
 * @param bool $logo logo文件名
 * @param bool $show 是否展示
 * @param bool $type 是否返回二维码路径
 * @param string $errorCorrectionLevel 容错级别
 * @param int $matrixPointSize 生成图片大小
 * @return string
 */
function create_qrcode($text,$filename=false,$logo=false,$show=false,$type=false,$errorCorrectionLevel='H',$matrixPointSize=10)
{
    require_once EXTEND_PATH .'phpqrcode/phpqrcode.php';
    if($filename)
        $filename = 'qrcode/'.$filename;

    $object = new \QRcode();
    $object->png($text,$filename, $errorCorrectionLevel, $matrixPointSize, 2);

    !$filename && exit();//直接输出到页面

    if ($logo !== FALSE) {
        $QR = $filename;//已经生成的原始二维码图
        $logo = 'qrcode/'.$logo;
        $QR = imagecreatefromstring(file_get_contents($QR));
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);//二维码图片宽度
        $QR_height = imagesy($QR);//二维码图片高度
        $logo_width = imagesx($logo);//logo图片宽度
        $logo_height = imagesy($logo);//logo图片高度
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width/$logo_qr_width;
        $logo_qr_height = $logo_height/$scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;
        //重新组合图片并调整大小
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        //输出图片
        imagepng($QR, $filename);
    }

    if($show)
        echo '<img src="/'.$filename.'">';

    if($type)
        return '/'.$filename;
}

/**
 * [is_really_writable  判断文件或目录是否有写的权限]
 * @author [忘尘]
 * @param $file
 * @return bool
 */
function is_really_writable($file)
{
    if (DIRECTORY_SEPARATOR == '/' AND @ ini_get("safe_mode") == FALSE)
    {
        return is_writable($file);
    }
    if (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE)
    {
        return FALSE;
    }

    fclose($fp);
    return TRUE;
}


/**
 * [ip_address  根据ip获取地点]
 * @author [忘尘]
 * @param string $ip
 * @return mixed
 */
function ip_address($ip = '')
{
    if(empty($ip))
        $ip = getIP();

    $Ip = new \com\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
    $ipdata = $Ip->getlocation($ip);

    return $ipdata['country'].$ip;
}

/**
 * [check_user_password  检查用户密码]
 * @author [忘尘]
 * @param $str
 * @return string
 */
function check_user_password($str){
    return  md5(md5($str) . config('user.user_password_halt'));
}

/**
 * 检查手机号码格式
 * @param $mobile 手机号码
 */
function check_mobile($mobile){
    if(preg_match('/1[34578]\d{9}$/',$mobile))
        return true;
    return false;
}

/**
 * 检查固定电话
 * @param $mobile
 * @return bool
 */
function check_telephone($mobile){
    if(preg_match('/^([0-9]{3,4}-)?[0-9]{7,8}$/',$mobile))
        return true;
    return false;
}

/**
 * 检查邮箱地址格式
 * @param $email 邮箱地址
 */
function check_email($email){
    if(filter_var($email,FILTER_VALIDATE_EMAIL))
        return true;
    return false;
}


/**
 * 获取客户端操作系统信息包括win10
 * @param  null
 * @author  Jea杨
 * @return string
 */
function get_os(){
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $os = false;

    if (preg_match('/win/i', $agent) && strpos($agent, '95'))
    {
        $os = 'Windows 95';
    }
    else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))
    {
        $os = 'Windows ME';
    }
    else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))
    {
        $os = 'Windows 98';
    }
    else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))
    {
        $os = 'Windows Vista';
    }
    else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))
    {
        $os = 'Windows 7';
    }
    else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))
    {
        $os = 'Windows 8';
    }else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))
    {
        $os = 'Windows 10';#添加win10判断
    }else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))
    {
        $os = 'Windows XP';
    }
    else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))
    {
        $os = 'Windows 2000';
    }
    else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))
    {
        $os = 'Windows NT';
    }
    else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))
    {
        $os = 'Windows 32';
    }
    else if (preg_match('/linux/i', $agent))
    {
        $os = 'Linux';
    }
    else if (preg_match('/unix/i', $agent))
    {
        $os = 'Unix';
    }
    else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))
    {
        $os = 'SunOS';
    }
    else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))
    {
        $os = 'IBM OS/2';
    }
    else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))
    {
        $os = 'Macintosh';
    }
    else if (preg_match('/PowerPC/i', $agent))
    {
        $os = 'PowerPC';
    }
    else if (preg_match('/AIX/i', $agent))
    {
        $os = 'AIX';
    }
    else if (preg_match('/HPUX/i', $agent))
    {
        $os = 'HPUX';
    }
    else if (preg_match('/NetBSD/i', $agent))
    {
        $os = 'NetBSD';
    }
    else if (preg_match('/BSD/i', $agent))
    {
        $os = 'BSD';
    }
    else if (preg_match('/OSF1/i', $agent))
    {
        $os = 'OSF1';
    }
    else if (preg_match('/IRIX/i', $agent))
    {
        $os = 'IRIX';
    }
    else if (preg_match('/FreeBSD/i', $agent))
    {
        $os = 'FreeBSD';
    }
    else if (preg_match('/teleport/i', $agent))
    {
        $os = 'teleport';
    }
    else if (preg_match('/flashget/i', $agent))
    {
        $os = 'flashget';
    }
    else if (preg_match('/webzip/i', $agent))
    {
        $os = 'webzip';
    }
    else if (preg_match('/offline/i', $agent))
    {
        $os = 'offline';
    }
    else
    {
        //$os = '未知操作系统';
        $os = get_device_type();
    }
    return $os;
}


/**
 * 获取客户端浏览器信息 添加win10 edge浏览器判断
 * @param  null
 * @author  Jea杨
 * @return string
 */
function get_broswer(){
    $sys = $_SERVER['HTTP_USER_AGENT'];  //获取用户代理字符串
    if (stripos($sys, "Firefox/") > 0) {
        preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
        $exp[0] = "Firefox";
        $exp[1] = $b[1];  //获取火狐浏览器的版本号
    } elseif (stripos($sys, "Maxthon") > 0) {
        preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou);
        $exp[0] = "傲游";
        $exp[1] = $aoyou[1];
    } elseif (stripos($sys, "MSIE") > 0) {
        preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
        $exp[0] = "IE";
        $exp[1] = $ie[1];  //获取IE的版本号
    } elseif (stripos($sys, "OPR") > 0) {
        preg_match("/OPR\/([\d\.]+)/", $sys, $opera);
        $exp[0] = "Opera";
        $exp[1] = $opera[1];
    } elseif(stripos($sys, "Edge") > 0) {
        //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
        preg_match("/Edge\/([\d\.]+)/", $sys, $Edge);
        $exp[0] = "Edge";
        $exp[1] = $Edge[1];
    } elseif (stripos($sys, "Chrome") > 0) {
        preg_match("/Chrome\/([\d\.]+)/", $sys, $google);
        $exp[0] = "Chrome";
        $exp[1] = $google[1];  //获取google chrome的版本号
    } elseif(stripos($sys,'rv:')>0 && stripos($sys,'Gecko')>0){
        preg_match("/rv:([\d\.]+)/", $sys, $IE);
        $exp[0] = "IE";
        $exp[1] = $IE[1];
    }else {
        //$exp[0] = "未知浏览器";
        //$exp[1] = "";

        //是否微信打开
        $exp[0] = is_wechat_open();
        $exp[1] = "";
        return $exp[0];
    }
    return $exp[0].'('.$exp[1].')';
}


/**
 * [alluser  所有会员]
 * @author [忘尘]
 * @return array 数组下标为userid
 */
function alluser(){
    $arr = [];
    $user = Db::name('user')->select();
    foreach($user as $k => $v)
        $arr[$v['id']] = $v;

    return $arr;
}


/**
 * [array_sort  多维数组 按某个键值排序]
 * @author [忘尘]
 * @param $array 要排序的数组
 * @param $keys 用来排序的键名
 * @param string $type 默认为升序排序
 * @return array
 */
function array_sort($array,$keys,$type='asc'){
    $keysvalue = $new_array = array();
    foreach ($array as $k=>$v){
        $keysvalue[$k] = $v[$keys];
    }
    if($type == 'asc'){
        asort($keysvalue);
    }else{
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach ($keysvalue as $k=>$v){
        $new_array[$k] = $array[$k];
    }
    return $new_array;
}


/**
 * [get_device_type  判断设备ios或android]
 * @author [忘尘]
 * @return string
 */
function get_device_type() {
    //全部变成小写字母
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $type = 'other';
    //分别进行判断
    if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
    {
        $type = 'ios';
    }
    if(strpos($agent, 'android'))
    {
        $type = 'android';
    }
    return $type;
}

/**
 * [is_wechat_open  是否微信打开]
 * @author [C.]
 * @return string
 */
function is_wechat_open()
{
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    if (strpos($user_agent, 'MicroMessenger') === false) {
        // 非微信浏览器禁止浏览
        //echo "HTTP/1.1 401 Unauthorized";
        //return '未知浏览器';
        return '其它浏览器';
    } else {
        // 微信浏览器，允许访问
        // 获取版本号
        preg_match('/.*?(MicroMessenger\/([0-9.]+))\s*/', $user_agent, $matches);
        return  '微信Version:'.$matches[2];
    }
}


/**
 * [create_captcha  自定义生成验证吧]
 * @author [忘尘]
 * @return \think\Response
 */
function create_captcha()
{
    $config = config('captcha.captcha');//验证码配置
    $captcha = new \think\captcha\Captcha($config);
    return $captcha->entry();
}

/**
 * [getMillisecond  获取毫秒级别的时间戳]
 * @author [忘尘]
 * @return array|string
 */
function getMillisecond()
{
    //获取毫秒的时间戳
    $time = explode ( " ", microtime () );
    $time = $time[1] . ($time[0] * 1000);
    $time2 = explode( ".", $time );
    $time = $time2[0];
    return $time;
}

/**
 * [is_from_mobileorpc  判断设备是手机还是PC]
 * @author [忘尘]
 * @return bool true为手机
 */
function is_from_mobileorpc()
{
    $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
    function CheckSubstrs($substrs,$text){
        foreach($substrs as $substr)
            if(false!==strpos($text,$substr)){
                return true;
            }
        return false;
    }
    $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
    $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');

    $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) || CheckSubstrs($mobile_token_list,$useragent);

    if ($found_mobile){
        return true;
    }else{
        return false;
    }
}


/**
 * http请求
 * @param  string  $url    请求地址
 * @param  boolean|string|array $params 请求数据
 * @param  integer $ispost 0/1，是否post
 * @param  array  $header
 * @param  $verify 是否验证ssl
 * return string|boolean          出错时返回false
 */
function httpUrl($url, $params = false, $ispost = 0, $header = array(), $verify = false) {
    $httpInfo = array();
    $ch = curl_init();
    if(!empty($header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //忽略ssl证书
    if($verify === true){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    } else {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if (is_array($params)) {
            $params = http_build_query($params);
        }
        if ($params) {
            file_put_contents('url.txt', $url . '?' . $params);
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $response = curl_exec($ch);
    if ($response === FALSE) {
        trace("cURL Error: " . curl_errno($ch) . ',' . curl_error($ch), 'error');
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        trace($httpInfo, 'error');
        return false;
    }
    curl_close($ch);
    return $response;
}


/**
 * [get_mobile_address 淘宝api  获取电话号码地址信息]
 * @author [忘尘]
 * @param string $mobile
 * @return bool|mixed
 */
function get_mobile_address($mobile = '')
{
    if (!is_numeric($mobile) || !isMobile($mobile)) {
        return false;
    }

    $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$mobile."&t=".time();
    $data = httpUrl($url);

    $ret = null;
    if (!empty($data)) {
        preg_match_all("/(\w+):'([^']+)/", $data, $res);
        $items = array_combine($res[1], $res[2]);
        foreach ($items as $itemKey => $itemVal) {
            $ret[$itemKey] = iconv('GB2312', 'UTF-8', $itemVal);//乱码转编码
        }
    }

    return $ret; //array(7) { ["mts"]=> string(7) "1375000" ["province"]=> string(6) "广东" ["catName"]=> string(12) "中国移动" ["telString"]=> string(11) "13750006078" ["areaVid"]=> string(5) "30517" ["ispVid"]=> string(7) "3236139" ["carrier"]=> string(12) "广东移动" }
}

/**
 * 整理菜单树方法
 * @param $param
 * @return array
 */
function prepareMenu($param)
{
    $parent = []; //父类
    $child = [];  //子类

    foreach($param as $key=>$vo){
        if($vo['pid'] == 0){
            $vo['href'] = '#';
            $parent[] = $vo; //一级菜单
        }else{
            $vo['href'] = url($vo['name']); //跳转地址
            $child[] = $vo; //一级菜单
        }
    }

    //从每一级菜单找到相应的二级菜单
    foreach($parent as $key=>$vo){
        foreach($child as $k=>$v){
            if($v['pid'] == $vo['id']){
                $parent[$key]['child'][] = $v;
            }
        }
    }

    unset($child);

    return $parent;
}

/**
 * 后台通用化数据输出
 * @param int $status 业务状态码
 * @param string $message 信息提示
 * @param [] $data  数据
 * @return array
 */
function admin_json($code, $msg, $data= []) {
    $join = [
        'code' => $code,
        'msg' => $msg,
        'data' => $data,
    ];
    return json($join);
}


//验证身份证
function is_idcard( $id )
{
    $id = strtoupper($id);
    $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
    $arr_split = array();
    if (!preg_match($regx, $id)) {
        return FALSE;
    }
    if (15 == strlen($id)) //检查15位
    {
        $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";

        @preg_match($regx, $id, $arr_split);
        //检查生日日期是否正确
        $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
        if (!strtotime($dtm_birth)) {
            return FALSE;
        } else {
            return TRUE;
        }
    } else      //检查18位
    {
        $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
        @preg_match($regx, $id, $arr_split);
        $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
        if (!strtotime($dtm_birth)) //检查生日日期是否正确
        {
            return FALSE;
        } else {
            //检验18位身份证的校验码是否正确。
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            $sign = 0;
            for ($i = 0; $i < 17; $i++) {
                $b = (int)$id[$i];
                $w = $arr_int[$i];
                $sign += $b * $w;
            }
            $n = $sign % 11;
            $val_num = $arr_ch[$n];
            if ($val_num != substr($id, 17, 1)) {
                return FALSE;
            } //phpfensi.com
            else {
                return TRUE;
            }
        }
    }
}